package com.zy.fee.roadpark.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.flyco.roundview.RoundLinearLayout;
import com.flyco.roundview.RoundTextView;
import com.zy.fee.roadpark.R;
import com.zy.fee.roadpark.model.M_Node;
import com.zygk.library.util.ColorUtil;
import com.zygk.library.util.DateTimeUtil;
import com.zygk.library.util.RxTextTool;
import com.zygk.library.view.AutoResizeTextView;
import java.util.List;

/**
 * Created by Wikison on 2018/9/15.
 */
public class NbNodeAdapter extends RecyclerView.Adapter<NbNodeAdapter.NodeViewHolder> {
  private Context mContext;
  private LayoutInflater mInflater;
  private List<M_Node> mDatas;
  private OnItemClickListener listener;

  public NbNodeAdapter(Context mContext, List<M_Node> datas, boolean isLoadMore) {
    this.mContext = mContext;
    this.mInflater = LayoutInflater.from(mContext);
    mDatas = datas;
  }

  public void setData(List<M_Node> list, boolean isLoadMore) {
    if (!isLoadMore) {
      mDatas.clear();
    }
    mDatas.addAll(list);
    notifyDataSetChanged();
  }

  public interface OnItemClickListener {
    void onItemClick(View view, M_Node mNode);
  }

  public void setOnItemClickListener(OnItemClickListener listener) {
    this.listener = listener;
  }

  @Override public NodeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    return new NodeViewHolder(mInflater.inflate(R.layout.item_nb_spot, parent, false));
  }

  @Override public void onBindViewHolder(NodeViewHolder holder, int position) {
    holder.bind(position);
  }

  @Override public int getItemCount() {
    return mDatas.size();
  }

  public class NodeViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    RoundTextView tvPlateNumber;
    AutoResizeTextView tvSpotNumber;
    RoundTextView tvCarState;
    RoundLinearLayout llSpotNumber;
    RoundLinearLayout llContainer;
    ImageView ivState;
    TextView tvTime;
    M_Node m;

    public NodeViewHolder(View itemView) {
      super(itemView);

      llContainer = (RoundLinearLayout) itemView.findViewById(R.id.rll_container);
      tvPlateNumber = (RoundTextView) itemView.findViewById(R.id.tv_plate_number);
      tvSpotNumber = (AutoResizeTextView) itemView.findViewById(R.id.tv_spot_number);
      tvCarState = (RoundTextView) itemView.findViewById(R.id.tv_car_state);
      llSpotNumber = (RoundLinearLayout) itemView.findViewById(R.id.ll_spot_number);
      tvTime = (TextView) itemView.findViewById(R.id.tv_time);
    }

    public void bind(int position) {
      itemView.setOnClickListener(this);
      m = mDatas.get(position);
      tvSpotNumber.setText("" + m.getCode());
      switch (m.getShowState()) {
        case 2:
          llContainer.getDelegate().setStrokeColor(ColorUtil.getColor(mContext, R.color.theme_red));
          tvPlateNumber.getDelegate()
              .setBackgroundColor(ColorUtil.getColor(mContext, R.color.theme_red));
          tvPlateNumber.setText("未登记车牌");
          if (m.getState() == 2) {
            llSpotNumber.getDelegate()
                .setStrokeColor(ColorUtil.getColor(mContext, R.color.theme_red));
            tvCarState.getDelegate()
                .setBackgroundColor(ColorUtil.getColor(mContext, R.color.theme_red));
            tvCarState.setText("故障");
          } else {
            llSpotNumber.getDelegate()
                .setStrokeColor(ColorUtil.getColor(mContext, R.color.theme_orange));
            tvCarState.getDelegate()
                .setBackgroundColor(ColorUtil.getColor(mContext, R.color.theme_orange));
            tvCarState.setText("有车");
          }
          tvTime.setText(DateTimeUtil.getSecondsFormat(m.getSeconds()));
          break;
        case 1:
          if (m.getIsMonth() == 1) {
            RxTextTool.getBuilder("", mContext)
                .append(m.getCarNumber())
                .append("(包月)")
                .setBold()
                .into(tvPlateNumber);
            llContainer.getDelegate()
                .setStrokeColor(ColorUtil.getColor(mContext, R.color.theme_blue));
            tvPlateNumber.getDelegate()
                .setBackgroundColor(ColorUtil.getColor(mContext, R.color.theme_blue));
          } else {
            RxTextTool.getBuilder("", mContext)
                .append(m.getCarNumber())
                .append("(临停)")
                .into(tvPlateNumber);
            llContainer.getDelegate()
                .setStrokeColor(ColorUtil.getColor(mContext, R.color.theme_color));
            tvPlateNumber.getDelegate()
                .setBackgroundColor(ColorUtil.getColor(mContext, R.color.theme_color));
          }
          if (m.getState() == 2) {
            llSpotNumber.getDelegate()
                .setStrokeColor(ColorUtil.getColor(mContext, R.color.theme_red));
            tvCarState.getDelegate()
                .setBackgroundColor(ColorUtil.getColor(mContext, R.color.theme_red));
            tvCarState.setText("故障");
          } else {
            llSpotNumber.getDelegate()
                .setStrokeColor(ColorUtil.getColor(mContext, R.color.theme_orange));
            tvCarState.getDelegate()
                .setBackgroundColor(ColorUtil.getColor(mContext, R.color.theme_orange));
            tvCarState.setText("有车");
          }
          tvTime.setText(DateTimeUtil.getSecondsFormat(m.getSeconds()));
          break;
        case 0:
          llContainer.getDelegate()
              .setStrokeColor(ColorUtil.getColor(mContext, R.color.font_black_999));
          tvPlateNumber.getDelegate()
              .setBackgroundColor(ColorUtil.getColor(mContext, R.color.font_black_999));
          tvPlateNumber.setText("车牌号");
          if (m.getState() == 2) {
            llSpotNumber.getDelegate()
                .setStrokeColor(ColorUtil.getColor(mContext, R.color.theme_red));
            tvCarState.getDelegate()
                .setBackgroundColor(ColorUtil.getColor(mContext, R.color.theme_red));
            tvCarState.setText("故障");
          } else {
            llSpotNumber.getDelegate()
                .setStrokeColor(ColorUtil.getColor(mContext, R.color.font_black_999));
            tvCarState.getDelegate()
                .setBackgroundColor(ColorUtil.getColor(mContext, R.color.font_black_999));
            tvCarState.setText("空闲");
          }

          tvTime.setText("00:00:00");
          break;
      }
    }

    @Override public void onClick(View v) {
      if (listener != null) listener.onItemClick(v, m);
    }
  }
}
